package gold.digger;

import gold.utils.InputUtil;
import gold.utils.UF;

import java.util.ArrayList;
import java.util.List;
import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC952 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int largestComponentSize(int[] nums) {
            Map<Integer, List<Integer>> memo = new HashMap<>();
            for (int i = 0; i < nums.length; i++) {
                final List<Integer> divisorList = getDivisors(nums[i]);
                for (Integer divisor : divisorList) {
                    if (!memo.containsKey(divisor)) memo.put(divisor, new ArrayList<>());
                    memo.get(divisor).add(i);
                }
            }

            UF uf = new UF(nums.length);
            for (Integer divisor : memo.keySet()) {
                if (memo.get(divisor).size() > 1) {
                    final Integer iStart = memo.get(divisor).get(0);
                    for (int i = 1; i < memo.get(divisor).size(); i++) {
                        uf.union(iStart, memo.get(divisor).get(i));
                    }
                }
            }

            int ans = 1;
            for (int wei : uf.size) {
                ans = Math.max(ans, wei);
            }

            return ans;
        }

        public class UF {
            // 连通分量个数
            private int count;
            // 存储一棵树
            private int[] parent;
            // 记录树的“重量”
            public int[] size;

            public UF(int n) {
                this.count = n;
                parent = new int[n];
                size = new int[n];
                for (int i = 0; i < n; i++) {
                    parent[i] = i;
                    size[i] = 1;
                }
            }

            /* 将 p 和 q 连接 */
            public void union(int p, int q) {
                int rootP = find(p);
                int rootQ = find(q);
                if (rootP == rootQ)
                    return;

                // 小树接到大树下面，较平衡
                if (size[rootP] > size[rootQ]) {
                    parent[rootQ] = rootP;
                    size[rootP] += size[rootQ];
                } else {
                    parent[rootP] = rootQ;
                    size[rootQ] += size[rootP];
                }
                count--;
            }

            /* 判断 p 和 q 是否连通 */
            public boolean connected(int p, int q) {
                int rootP = find(p);
                int rootQ = find(q);
                return rootP == rootQ;
            }

            private int find(int x) {
                while (parent[x] != x) {
                    // 进行路径压缩
                    parent[x] = parent[parent[x]];
                    x = parent[x];
                }
                return x;
            }

            /* 返回图中有多少个连通分量 */
            public int count() {
                return count;
            }

            /* 返回图中联通集合 */
            public Map<Integer, List<Integer>> outputAggregateSet(int n) {
                Map<Integer, List<Integer>> aggregateMap = new HashMap<>();

                // 共有n个集合
                for (int i = 0; i < n; i++) {
                    int parentId = find(i);
                    if (!aggregateMap.containsKey(parentId)) aggregateMap.put(parentId, new ArrayList<>());
                    aggregateMap.get(parentId).add(i);
                }

                System.out.println(aggregateMap.toString());
                return aggregateMap;
            }
        }

        /**
         * 得到一个数的全部素数因子
         *
         * @param data 传入一个整数
         * @return 返回素数因子
         */
        private List<Integer> getDivisors(int data) {
            List<Integer> primeList = new ArrayList<>();
            int k = Math.abs(data);
            int sum = 1;
            if (!isPrime(data)) { //如果这个数是素数的话，直接返回
                int len = (int) Math.sqrt(Math.abs(data));
                for (int i = 2; i <= len; i++) { //任何一个数都可以分解为素数因子的乘积
                    if (isPrime(i)) {
                        while (data % i == 0) {
                            sum *= i;
                            if (sum <= k) {
                                primeList.add(i);
                            }
                            data = data / i;
                            if (isPrime(data)) {
                                primeList.add(data);
                                sum *= data;
                                break;
                            }
                        }
                        if (sum == k)
                            break;
                    }
                }
            } else {
                primeList.add(data);
            }
            return primeList;
        }

        private boolean isPrime(int data) {
            for (int i = 2; i <= Math.sqrt(Math.abs(data)); i++) {
                if (data % i == 0)
                    return false;
            }
            return true;
        }
    }

    public void run() {
        System.out.println(new Solution().largestComponentSize(InputUtil.toIntegerArray("[4,6,15,35]")));
        System.out.println(new Solution().largestComponentSize(InputUtil.toIntegerArray("[20,50,9,63]")));
        System.out.println(new Solution().largestComponentSize(InputUtil.toIntegerArray("[2,3,6,7,4,12,21,39]")));
    }

    public static void main(String[] args) throws Exception {
        LC952 an = new LC952();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
